package edu.fiu.cis.personalize.server.berkDB.serializers;

import java.util.HashSet;
import java.util.Set;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

public abstract class SetSerializer<E> extends TupleBinding<Set<E>> {

	@Override
	public Set<E> entryToObject(TupleInput ti) {
		int size = ti.readInt();
		Set<E> set = new HashSet<E>();
		for (int i = 0; i < size; i++) {
			set.add(readElement(ti));
		}
		return set;
	}

	@Override
	public void objectToEntry(Set<E> set, TupleOutput to) {
		to.writeInt(set.size());
		for (E s : set) {
			writeElement(to, s);
		}
	}

	protected abstract void writeElement(TupleOutput to, E s);

	protected abstract E readElement(TupleInput ti);

}
